package com.example.icongyou.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.icongyou.R;
import com.example.icongyou.bean.Course;
import com.example.icongyou.bean.StudentCourseProgress;

import java.util.List;

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {

    private final List<Course> courseList;
    private final List<StudentCourseProgress> progressList;
    private final OnCourseClickListener listener;

    public interface OnCourseClickListener {
        void onCourseClick(Course course);
    }

    public CourseAdapter(List<Course> courseList, List<StudentCourseProgress> progressList, OnCourseClickListener listener) {
        this.courseList = courseList;
        this.progressList = progressList;
        this.listener = listener;
    }

    @NonNull
// 在CourseAdapter的onCreateViewHolder中
    @Override
    public CourseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_course_card_teacher, parent, false);
        return new CourseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
        Course course = courseList.get(position);
        StudentCourseProgress progress = findProgressByCourseId(course.getCourseId());

        // 绑定课程基本信息
        holder.tvCourseName.setText(course.getCourseName());
        holder.tvTeacherName.setText(course.getTeacherName());
        holder.tvTotalLessons.setText(course.getTotalLessons() + "课时");

        // 绑定进度信息
        if (progress != null) {
            int progressPercent = (int) (progress.getProgressRate() * 100);
            holder.pbCourseProgress.setProgress(progressPercent);
            holder.tvProgress.setText(progress.getCompletedLessons() + "/" +
                    course.getTotalLessons() + " (" + progressPercent + "%)");
        } else {
            holder.pbCourseProgress.setProgress(0);
            holder.tvProgress.setText("0/" + course.getTotalLessons() + " (0%)");
        }

        // 课程卡片点击事件
        holder.itemView.setOnClickListener(v -> listener.onCourseClick(course));
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    // 通过课程ID匹配进度信息
    private StudentCourseProgress findProgressByCourseId(String courseId) {
        for (StudentCourseProgress progress : progressList) {
            if (progress.getCourseId().equals(courseId)) {
                return progress;
            }
        }
        return null;
    }

    // 进度百分比文本
    public class CourseViewHolder extends RecyclerView.ViewHolder {
        TextView tvCourseName;
        TextView tvTeacherName;
        TextView tvTotalLessons;
        ProgressBar pbCourseProgress;
        TextView tvProgress;

        public CourseViewHolder(@NonNull View itemView) {
            super(itemView);
            // 绑定布局控件ID（需与item_course_card.xml中的ID对应）
            tvCourseName = itemView.findViewById(R.id.tv_course_name);
            tvTeacherName = itemView.findViewById(R.id.tv_teacher_name);
            tvTotalLessons = itemView.findViewById(R.id.tv_total_lessons);
            pbCourseProgress = itemView.findViewById(R.id.pb_course_progress);
            tvProgress = itemView.findViewById(R.id.tv_progress);
        }
    }
}